/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vertexnet.realestate.view.panel;

import static com.vertexnet.realestate.constants.UIConstants.ADMIN;

import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import com.vertexnet.realestate.view.utility.DefaultTable;

import com.vertexnet.realestate.interfaces.Controller;
import com.vertexnet.realestate.interfaces.DefaultPanel;
import com.vertexnet.realestatenet.model.bean.Area;
import com.vertexnet.realestatenet.model.bean.SecurityQuestion;

/**
 *
 * @author Srivathsa
 */
public class AdminPanel extends javax.swing.JPanel implements DefaultPanel {

	private DefaultListModel  areasListModel;
	private DefaultListModel securityQuestionsModel;
	private static final long serialVersionUID = 1L;
	private List<Area> areasToAdd = new ArrayList<Area>();
	private List<SecurityQuestion> securityQuestionsToAdd = new ArrayList<SecurityQuestion>();
	private List<Area> areasToDelete = new ArrayList<Area>();
	private List<SecurityQuestion> securityQuestionsToDelete = new ArrayList<SecurityQuestion>();
	private Controller controller;
	
	/**
     * Creates new form AdminPanel
     */
    public AdminPanel(Controller controller) {
        initComponents();
        this.controller = controller;
        jPanel1.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        securityQuestionsList = new javax.swing.JList();
        addSecurityQuestionButton = new javax.swing.JButton();
        deleteSQButton = new javax.swing.JButton();
        sQInputTextField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        agentsList = new javax.swing.JList();
        addAreasButton = new javax.swing.JButton();
        deleteAreasButton = new javax.swing.JButton();
        areasInputTextField = new javax.swing.JTextField();
        CancelAdminChangesButton = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Security Questions"));

        jScrollPane1.setViewportView(securityQuestionsList);

        addSecurityQuestionButton.setText("Add");
        addSecurityQuestionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSecurityQuestionButtonActionPerformed(evt);
            }
        });

        deleteSQButton.setText("Delete");
        deleteSQButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteSQButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(deleteSQButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(addSecurityQuestionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(sQInputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(137, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addSecurityQuestionButton, deleteSQButton, sQInputTextField});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(sQInputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addSecurityQuestionButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteSQButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        saveButton.setText("Save");
        saveButton.setName(ADMIN.getSave());
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Areas"));

        jScrollPane2.setViewportView(agentsList);

        addAreasButton.setText("Add");
        addAreasButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAreasButtonActionPerformed(evt);
            }
        });

        deleteAreasButton.setText("Delete");
        deleteAreasButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteAreasButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(deleteAreasButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(addAreasButton, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(areasInputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(138, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addAreasButton, areasInputTextField, deleteAreasButton});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(areasInputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addAreasButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteAreasButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        CancelAdminChangesButton.setText("Cancel");
        CancelAdminChangesButton.setName(ADMIN.getReport());
        CancelAdminChangesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelAdminChangesButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CancelAdminChangesButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 151, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(CancelAdminChangesButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addAreasButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAreasButtonActionPerformed
    	String text = areasInputTextField.getText();
    	if(text.trim().isEmpty()) {
    		JOptionPane.showMessageDialog(this, "Area Name Cannot Be Empty");
    	}
    	Area area = new Area();
    	area.setAreaName(text);
    	for(int i=0 ; i< areasListModel.size(); i++) {
    		if(areasListModel.get(i).toString().trim().equals(text)) {
    			JOptionPane.showMessageDialog(this, "An Area with Same Name Exists");
    			return;
    		}
    	}
    	areasToAdd.add(area);
    	areasListModel.addElement(area);
    }//GEN-LAST:event_addAreasButtonActionPerformed

	private void deleteAreasButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_deleteAreasButtonActionPerformed
		Area area = null;
		if (areasListModel.getSize() > 0) {
			area = (Area) agentsList.getSelectedValue();
		}
		if(area != null) {
			areasToAdd.remove(area);
			if(area.getAreaId() != null) {
				areasToDelete.add(area);
			}
			areasListModel.removeElement(area);
		}
	}// GEN-LAST:event_deleteAreasButtonActionPerformed

    private void addSecurityQuestionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSecurityQuestionButtonActionPerformed
    	String text = sQInputTextField.getText();
    	if(text.trim().isEmpty()) {
    		JOptionPane.showMessageDialog(this, "Area Name Cannot Be Empty");
    	}
    	for(int i=0 ; i< securityQuestionsModel.size(); i++) {
    		if(securityQuestionsModel.get(i).toString().trim().equals(text)) {
    			JOptionPane.showMessageDialog(this, "Same Question Exists");
    			return;
    		}
    	}
    	SecurityQuestion securityQuestion = new SecurityQuestion();
    	securityQuestion.setQuestion(text);
    	
    	securityQuestionsToAdd.add(securityQuestion);
    	securityQuestionsModel.addElement(securityQuestion);
    }//GEN-LAST:event_addSecurityQuestionButtonActionPerformed

    private void deleteSQButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteSQButtonActionPerformed
		SecurityQuestion securityQuestion = null;
		if (areasListModel.getSize() > 0) {
			securityQuestion = (SecurityQuestion) securityQuestionsList.getSelectedValue();
		}
		if(securityQuestion != null) {
			securityQuestionsToAdd.remove(securityQuestion);
			if(securityQuestion.getQuestionId() != 0) {
				securityQuestionsToDelete.add(securityQuestion);
			}
			securityQuestionsModel.removeElement(securityQuestion);
		}
    }//GEN-LAST:event_deleteSQButtonActionPerformed

    private void CancelAdminChangesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelAdminChangesButtonActionPerformed
    	controller.handleEvent(evt);
    	JOptionPane.showMessageDialog(this, "Cancelled Changes");
    	this.areasInputTextField.setText("");
    	this.sQInputTextField.setText("");
    }//GEN-LAST:event_CancelAdminChangesButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
    	controller.handleEvent(evt);
    	JOptionPane.showMessageDialog(this, "Changes added/deleted Successfully");
    	this.areasInputTextField.setText("");
    	this.sQInputTextField.setText("");
    }//GEN-LAST:event_saveButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelAdminChangesButton;
    private javax.swing.JButton addAreasButton;
    private javax.swing.JButton addSecurityQuestionButton;
    private javax.swing.JList agentsList;
    private javax.swing.JTextField areasInputTextField;
    private javax.swing.JButton deleteAreasButton;
    private javax.swing.JButton deleteSQButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField sQInputTextField;
    private javax.swing.JButton saveButton;
    private javax.swing.JList securityQuestionsList;
    // End of variables declaration//GEN-END:variables

	public JButton getAddButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public JButton getEditButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public JButton getDeleteButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public DefaultTable getTable() {
		// TODO Auto-generated method stub
		return null;
	}

	public JButton getReportButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Area> getAreasToAdd() {
		return areasToAdd;
	}

	public List<Area> getAreasToDelete() {
		return areasToDelete;
	}

	public List<SecurityQuestion> getSecurityQuestionsToAdd() {
		return securityQuestionsToAdd;
	}

	public List<SecurityQuestion> getSecurityQuestionsToDelete() {
		return securityQuestionsToDelete;
	}

	public void setAreas(List<Area> areas) {
    	areasListModel = new DefaultListModel();
    	for(Area area : areas) {
    		areasListModel.addElement(area);
    	}
    	agentsList.setModel(areasListModel);
	}

	public void setSecurityQuestions(List<SecurityQuestion> securityQuestions) {
		securityQuestionsModel = new DefaultListModel();
    	for(SecurityQuestion question : securityQuestions) {
    		securityQuestionsModel.addElement(question);
    	}
    	securityQuestionsList.setModel(securityQuestionsModel);
	}

	public JButton getMakePaymentButton() {
		// TODO Auto-generated method stub
		return null;
	}
}
